package com.zmc.coupon.controller;

import com.zmc.common.annotation.PreventRepeatSubmit;
import com.zmc.common.domain.es.EsSkuAttr;
import com.zmc.common.domain.es.EsSkuEntity;
import com.zmc.common.enums.RequestSourceEnum;
import com.zmc.common.utils.PageUtils;
import com.zmc.common.utils.R;
import com.zmc.common.utils.poi.ExcelUtil;
import com.zmc.coupon.infrastructure.bean.query.CouponSkuQuery;
import com.zmc.coupon.infrastructure.bean.vo.CouponSkuVo;
import com.zmc.coupon.infrastructure.entity.CouponSkuEntity;
import com.zmc.coupon.infrastructure.service.CouponSkuService;
import io.seata.common.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 优惠券商品信息
 *
 * @author zmc
 * @email 
 * @date 2024-10-09 14:59:36
 */
@RestController
@RequestMapping("/coupon/spu")
public class CouponSkuController {
    @Autowired
    private CouponSkuService couponSkuService;

    /**
     * 列表
     */
    @PostMapping("/list")
    public R list(@RequestBody CouponSkuQuery query){
        PageUtils page = couponSkuService.queryPage(query);
        return R.page(page);
    }

    /**
     * 导出列表
     */
    @PostMapping("/export")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public void export(HttpServletResponse response, CouponSkuQuery query) {
        List<CouponSkuEntity> list = couponSkuService.queryList(query);
        ExcelUtil<CouponSkuEntity> util = new ExcelUtil<>(CouponSkuEntity.class);
        util.exportExcel(response, list, "优惠券商品信息数据");
    }


    /**
     * 信息
     */
    @PostMapping("/info")
    public R info(@RequestBody CouponSkuVo vo){
        return R.ok(couponSkuService.getById(vo.getId()));
    }

    /**
     * 保存
     */
    @PostMapping("/add")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R save(@RequestBody CouponSkuVo vo){
        Long couponId = vo.getCouponId();
        List<EsSkuEntity> list = vo.getSkuList();
        List<String> skuIds = list.stream().map(EsSkuEntity::getSkuId).collect(Collectors.toList());
        List<CouponSkuEntity> couponSkuList = couponSkuService.getBySkuIds(skuIds);
        List<CouponSkuEntity> skuList = new ArrayList<>();
        list.forEach(item -> {
            long count = couponSkuList.stream().filter(coupon -> Objects.equals(coupon.getSkuId(), item.getSkuId())).count();
            if (count == 0) {
                CouponSkuEntity entity = new CouponSkuEntity();
                entity.setCouponId(couponId);
                entity.setSpuId(item.getSpuId());
                entity.setSpuName(item.getSpuTitle());
                entity.setSkuId(item.getSkuId());
                entity.setSkuAttr(item.getAttrs().stream().map(EsSkuAttr::getPropertyValue).collect(Collectors.joining(" ")));
                skuList.add(entity);
            }
        });
        if (CollectionUtils.isNotEmpty(skuList)) {
            couponSkuService.saveBatch(skuList);
        }
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @PreventRepeatSubmit(lockByPerson = true, source = RequestSourceEnum.MANAGE)
    public R remove(@RequestBody CouponSkuVo vo) {
        couponSkuService.removeById(vo.getId());
        return R.ok();
    }

}
